// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Finder.java

package presenter;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

// Referenced classes of package presenter:
//            SongDisplayerFrame, SongFile, Scheduler

public class Finder extends JFrame implements ActionListener, MouseListener {
    
    public static final long serialVersionUID=2;
    
    public Finder(int x, int y, int width, int height, SongFile database[], Scheduler schedule)
    {
        super("Song Finder");
        this.database = database;
        this.schedule = schedule;
        jp = new JPanel();
        jp.setLayout(new BorderLayout());
        search = new JTextField();
        JLabel label = new JLabel("Search string");
        sd = new SongDisplayerFrame();
        JPanel top = new JPanel(new GridLayout(1, 3));
        doSearch = new JButton("Search");
        doSearch.addActionListener(this);
        top.add(label);
        top.add(search);
        top.add(doSearch);
        list = new JList();
        list.addMouseListener(this);
        jp.add(top, "North");
        jp.add(list, "West");
        jp.add(sd, "Center");
        JPanel bottom = new JPanel(new GridLayout(1, 2));
        add = new JButton("Add Song");
        close = new JButton("Close");
        add.addActionListener(this);
        close.addActionListener(this);
        bottom.add(add);
        bottom.add(close);
        jp.add(bottom, "South");
        setLocation(x, y);
        setSize(width, height);
        setDefaultCloseOperation(2);
        setContentPane(jp);
        setVisible(true);
    }

    private SongFile[] findSongs(String lyrics)
    {
        ArrayList<SongFile> songs = new ArrayList<SongFile>();
        for(int i = 0; i < database.length; i++)
        {
            boolean hasLyrics = false;
            SongFile current = database[i];
            for(int j = 0; j < current.getNumberOfVerses(); j++)
            {
                String verses[] = current.getVerse(j).getLyrics();
                for(int k = 0; k < verses.length; k++)
                    if(verses[k].toUpperCase().contains(lyrics))
                        hasLyrics = true;

            }

            if(hasLyrics)
            {
                songs.add(current);
                boolean flag = false;
            }
        }

        return (SongFile[])songs.toArray(new SongFile[1]);
    }

    public static void main(String args[])
    {
        new Finder(100, 100, 800, 600, null, null);
    }

    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == doSearch)
        {
            results = findSongs(search.getText().toUpperCase());
            list.setListData(results);
        } else
        if(event.getSource() == list)
            sd.setSong(results[list.getSelectedIndex()]);
        else
        if(event.getSource() == add)
            schedule.addSong(results[list.getSelectedIndex()]);
        else
        if(event.getSource() == close)
            dispose();
    }

    public void mouseClicked(MouseEvent mouseevent)
    {
    }

    public void mousePressed(MouseEvent e)
    {
        if(e.getSource() == list)
            sd.setSong(results[list.getSelectedIndex()]);
    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
    }

    public void mouseExited(MouseEvent mouseevent)
    {
    }

    JPanel jp;
    JTextField search;
    SongFile database[];
    JButton doSearch;
    JButton add;
    JButton close;
    JList list;
    SongFile results[];
    SongDisplayerFrame sd;
    Scheduler schedule;
}
